{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6 计算和控制流（二）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.水仙花数判定"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建一个函数，接受一个参数 n (n >= 100)，判断这个数是否为水仙花数。\n",
    "\n",
    "> 水仙花数：满足如果这个数为 m 位数，则每个位上的数字的 m 次幂之和等于它本身\n",
    "\n",
    "*例如：1^3 + 5^3 + 3^3 = 153, 1^4 + 6^4 + 3^4 + 4^4 = 1634*\n",
    "\n",
    "结果返回 `True` 或者 `False`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "def isnarcissus(n):\n",
    "    count = 0  # 判断共有几位数\n",
    "    num = []   # 存储各位的数字\n",
    "    tmp1 = n\n",
    "    tmp2 = n\n",
    "    \n",
    "    # 求位数\n",
    "    while tmp1 != 0:\n",
    "        tmp1 //= 10\n",
    "        count += 1\n",
    "    \n",
    "    # 求各位上的数字\n",
    "    for i in range(count):\n",
    "        last = tmp2 % 10\n",
    "        num.append(last)\n",
    "        tmp2 //= 10\n",
    "    \n",
    "    # 判断是否为水仙花数\n",
    "    total = sum(list(map(lambda x: x ** count, num)))  # 求 count 次幂和\n",
    "    if total == n:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "    \n",
    "print(isnarcissus(371))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建一个函数，接受一个参数 max (max >= 1000)，调用上体编写的函数，求 100 到 max 之间的水仙花数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "153\n",
      "370\n",
      "371\n",
      "407\n",
      "1634\n"
     ]
    }
   ],
   "source": [
    "def find_narcissus(n_max):\n",
    "    for i in range(100, n_max + 1):\n",
    "        if isnarcissus(i):\n",
    "            print(i)\n",
    "\n",
    "            \n",
    "n_max = 2500\n",
    "find_narcissus(n_max)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.字符集合的并集"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建一个函数，接受两个字符串作为参数，返回两个字符串字符集合的并集。\n",
    "\n",
    "*例如：接受的两个字符串为 `\"abc\"` 和 `\"bcd\"`，返回 `set(['a', 'b', 'c', 'd'])`*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd']\n"
     ]
    }
   ],
   "source": [
    "def union_set(astr, bstr):\n",
    "    aset = set(astr)\n",
    "    bset = set(bstr)\n",
    "    cset = aset | bset  # 并集运算\n",
    "    return sorted(cset)\n",
    "\n",
    "\n",
    "astr = 'abc'\n",
    "bstr = 'bcd'\n",
    "print(union_set(astr, bstr))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
